package com.unei.redis.sharded;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 15-6-1.
 */
public class ShardedRedis {
    public static void main(String[] args){
        GenericObjectPoolConfig config=new GenericObjectPoolConfig();
        config.setMaxTotal(1000);
        config.setMaxIdle(500);

        List<JedisShardInfo> jedisShardInfoList=new ArrayList<JedisShardInfo>();
        JedisShardInfo shardInfo1=new JedisShardInfo("192.168.217.157",6380);
        JedisShardInfo shardInfo2=new JedisShardInfo("192.168.217.157",6381);
        JedisShardInfo shardInfo3=new JedisShardInfo("192.168.217.157",6382);
        jedisShardInfoList.add(shardInfo1);
        jedisShardInfoList.add(shardInfo2);
        jedisShardInfoList.add(shardInfo3);

        ShardedJedisPool pool=new ShardedJedisPool(config,jedisShardInfoList);

        set("user1","a",pool);
        set("user12","a",pool);
        set("user13","a",pool);
        set("usera","a",pool);
        set("userb","a",pool);
    }

    public static void set(String key,String value,ShardedJedisPool pool){
        ShardedJedis shardedJedis=pool.getResource();
        shardedJedis.set(key,value);
        pool.returnResource(shardedJedis);
    }
}
